package com.ruoyi.gis.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.gis.domain.GisLayer;
import com.ruoyi.gis.domain.GisLayerProduct;
import com.ruoyi.gis.domain.GisLayerProductGroup;
import com.ruoyi.gis.domain.GisLayerProductVo;
import com.ruoyi.gis.service.IGisLayerProductGroupService;
import com.ruoyi.gis.service.IGisLayerProductService;
import com.ruoyi.gis.service.IGisLayerService;
import com.ruoyi.gis.vo.AddGroupVo;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("authed/gis/product")
public class GislayerProductOutterController extends BaseController {
    @Autowired
    private IGisLayerProductService gisLayerProductService;
    @Autowired
    private IGisLayerProductGroupService gisLayerProductGroupService;
    @Autowired
    private ISysUserService userService;
    @Autowired
    private IGisLayerService gisLayerService;

    //查询用户所有图层
    @GetMapping("/{userAccount}/gisLayers")
    public AjaxResult listAllGisLayers(@PathVariable("userAccount") String userAccount, HttpServletRequest request) {
        String accessKey = request.getHeader("accessKey");
        String secretKey = request.getHeader("secretKey");
        if (!"fd1UlFrXXrqFW6YImGmj34Ad33f".equals(accessKey)) {
            return AjaxResult.error("授权失败");
        }
        if (!"FJifJR9gkHC6t6OMq7ELLIEZQ5Q".equals(secretKey)) {
            return AjaxResult.error("授权失败");
        }
        SysUser sysUser = userService.selectUserByUserName(userAccount);
        if (sysUser == null) {
            return AjaxResult.error("用户账号认证设备");
        }
        GisLayer gisLayer = new GisLayer();
        gisLayer.setUserId(sysUser.getUserId());
        List<GisLayer> list = gisLayerService.selectGisLayerList(gisLayer);
        return AjaxResult.success(list);
    }

    //查询用户图层数据产品
    @GetMapping("/{userAccount}/list")
    public AjaxResult listAllGroup(@PathVariable("userAccount") String userAccount, HttpServletRequest request) {
        String accessKey = request.getHeader("accessKey");
        String secretKey = request.getHeader("secretKey");
        if (!"fd1UlFrXXrqFW6YImGmj34Ad33f".equals(accessKey)) {
            return AjaxResult.error("授权失败");
        }
        if (!"FJifJR9gkHC6t6OMq7ELLIEZQ5Q".equals(secretKey)) {
            return AjaxResult.error("授权失败");
        }
        SysUser sysUser = userService.selectUserByUserName(userAccount);
        if (sysUser == null) {
            return AjaxResult.error("用户账号认证设备");
        }
        GisLayerProductGroup groupQuery = new GisLayerProductGroup();
        groupQuery.setUserId(sysUser.getUserId());
        List<GisLayerProductGroup> result = gisLayerProductGroupService
                .selectGisLayerProductGroupList(groupQuery);
        GisLayerProduct query = new GisLayerProduct();
        if (result != null && result.size() > 0) {
            result.stream().forEach(group -> {
                query.setUserId(sysUser.getUserId());
                query.setGroupId(group.getId());
                List<GisLayerProduct> list = gisLayerProductService.
                        selectGisLayerProductList(query);
                group.setLayerProducts(list);
            });
        }
        return AjaxResult.success(result);
    }

    ///////////////////////////////////////////////////////////////
    //添加数据产品组
    @PostMapping("/{userAccount}/addGroup")
    public AjaxResult addGroup(@PathVariable("userAccount") String userAccount,
                               @RequestBody AddGroupVo addGroupVo,
                               HttpServletRequest request) {
        String accessKey = request.getHeader("accessKey");
        String secretKey = request.getHeader("secretKey");
        if (!"fd1UlFrXXrqFW6YImGmj34Ad33f".equals(accessKey)) {
            return AjaxResult.error("授权失败");
        }
        if (!"FJifJR9gkHC6t6OMq7ELLIEZQ5Q".equals(secretKey)) {
            return AjaxResult.error("授权失败");
        }
        SysUser sysUser = userService.selectUserByUserName(userAccount);
        if (sysUser == null) {
            return AjaxResult.error("用户账号认证设备");
        }
        GisLayerProductGroup group = new GisLayerProductGroup();
        group.setGroupName(addGroupVo.getGroupName());
        group.setSort(addGroupVo.getSort());
        group.setUserId(sysUser.getUserId());
        gisLayerProductGroupService.insertGisLayerProductGroup(group);
        return AjaxResult.success();
    }


    //删除数据产品组
    @PostMapping("/{userAccount}/deleteGroup/{groupId}")
    public AjaxResult deleteGroup(@PathVariable("userAccount") String userAccount,
                                         @PathVariable("groupId") Long groupId,
                                      HttpServletRequest request) {
        String accessKey = request.getHeader("accessKey");
        String secretKey = request.getHeader("secretKey");
        if (!"fd1UlFrXXrqFW6YImGmj34Ad33f".equals(accessKey)) {
            return AjaxResult.error("授权失败");
        }
        if (!"FJifJR9gkHC6t6OMq7ELLIEZQ5Q".equals(secretKey)) {
            return AjaxResult.error("授权失败");
        }
        SysUser sysUser = userService.selectUserByUserName(userAccount);
        if (sysUser == null) {
            return AjaxResult.error("用户账号认证设备");
        }
        gisLayerProductGroupService.deleteGisLayerProductGroupById(groupId);
        return AjaxResult.success();
    }
    //数据产品组排序
    @PostMapping("/{userAccount}/sortGroup")
    public AjaxResult updateSort(@PathVariable("userAccount") String userAccount,
                                 @RequestBody List<GisLayerProductGroup> groups,
                                 HttpServletRequest request)
    {
        String accessKey = request.getHeader("accessKey");
        String secretKey = request.getHeader("secretKey");
        if (!"fd1UlFrXXrqFW6YImGmj34Ad33f".equals(accessKey)) {
            return AjaxResult.error("授权失败");
        }
        if (!"FJifJR9gkHC6t6OMq7ELLIEZQ5Q".equals(secretKey)) {
            return AjaxResult.error("授权失败");
        }
        SysUser sysUser = userService.selectUserByUserName(userAccount);
        if (sysUser == null) {
            return AjaxResult.error("用户账号认证设备");
        }
        return toAjax(gisLayerProductGroupService.updateGisLayerProductGroupSort(groups));
    }

    //更新
    @PostMapping("/{userAccount}/updateGroup")
    public AjaxResult updateChecked(@PathVariable("userAccount") String userAccount,
                                    @RequestBody GisLayerProductGroup gisLayerProductGroup,
                                    HttpServletRequest request)
    {
        String accessKey = request.getHeader("accessKey");
        String secretKey = request.getHeader("secretKey");
        if (!"fd1UlFrXXrqFW6YImGmj34Ad33f".equals(accessKey)) {
            return AjaxResult.error("授权失败");
        }
        if (!"FJifJR9gkHC6t6OMq7ELLIEZQ5Q".equals(secretKey)) {
            return AjaxResult.error("授权失败");
        }
        SysUser sysUser = userService.selectUserByUserName(userAccount);
        if (sysUser == null) {
            return AjaxResult.error("用户账号认证设备");
        }
        return toAjax(gisLayerProductGroupService.updateGisLayerProductGroupChecked(gisLayerProductGroup));
    }
    /////////////////////////////////////////////////////

    @PostMapping("/{userAccount}/addGroupProduct")
    public AjaxResult addGroupProduct(@PathVariable("userAccount") String userAccount,
                                      @RequestBody GisLayerProductVo addGroupVo,
                                      HttpServletRequest request) {
        String accessKey = request.getHeader("accessKey");
        String secretKey = request.getHeader("secretKey");
        if (!"fd1UlFrXXrqFW6YImGmj34Ad33f".equals(accessKey)) {
            return AjaxResult.error("授权失败");
        }
        if (!"FJifJR9gkHC6t6OMq7ELLIEZQ5Q".equals(secretKey)) {
            return AjaxResult.error("授权失败");
        }
        SysUser sysUser = userService.selectUserByUserName(userAccount);
        if (sysUser == null) {
            return AjaxResult.error("用户账号认证设备");
        }
        gisLayerProductService.addProduct(sysUser.getUserId(),addGroupVo);
        return AjaxResult.success();
    }
    @PostMapping("/{userAccount}/deleteProducts/{productId}")
    public AjaxResult deleteGroupProduct(@PathVariable("userAccount") String userAccount,
                                         @PathVariable("productId") Long productId,
                                         HttpServletRequest request) {
        String accessKey = request.getHeader("accessKey");
        String secretKey = request.getHeader("secretKey");
        if (!"fd1UlFrXXrqFW6YImGmj34Ad33f".equals(accessKey)) {
            return AjaxResult.error("授权失败");
        }
        if (!"FJifJR9gkHC6t6OMq7ELLIEZQ5Q".equals(secretKey)) {
            return AjaxResult.error("授权失败");
        }
        SysUser sysUser = userService.selectUserByUserName(userAccount);
        if (sysUser == null) {
            return AjaxResult.error("用户账号认证设备");
        }
        gisLayerProductService.deleteGisLayerProductById(productId);
        return AjaxResult.success();
    }
    @PostMapping("/{userAccount}/updateProduct")
    public AjaxResult edit(@PathVariable("userAccount") String userAccount,
                           @RequestBody GisLayerProduct gisLayerProduct,
                           HttpServletRequest request)
    {
        String accessKey = request.getHeader("accessKey");
        String secretKey = request.getHeader("secretKey");
        if (!"fd1UlFrXXrqFW6YImGmj34Ad33f".equals(accessKey)) {
            return AjaxResult.error("授权失败");
        }
        if (!"FJifJR9gkHC6t6OMq7ELLIEZQ5Q".equals(secretKey)) {
            return AjaxResult.error("授权失败");
        }
        SysUser sysUser = userService.selectUserByUserName(userAccount);
        if (sysUser == null) {
            return AjaxResult.error("用户账号认证设备");
        }
        return toAjax(gisLayerProductService.updateGisLayerProduct(gisLayerProduct));
    }

    @PostMapping("/{userAccount}/sortProducts")
    public AjaxResult sort(@PathVariable("userAccount") String userAccount,
                           @RequestBody List<GisLayerProduct> products,
                           HttpServletRequest request)
    {
        String accessKey = request.getHeader("accessKey");
        String secretKey = request.getHeader("secretKey");
        if (!"fd1UlFrXXrqFW6YImGmj34Ad33f".equals(accessKey)) {
            return AjaxResult.error("授权失败");
        }
        if (!"FJifJR9gkHC6t6OMq7ELLIEZQ5Q".equals(secretKey)) {
            return AjaxResult.error("授权失败");
        }
        SysUser sysUser = userService.selectUserByUserName(userAccount);
        if (sysUser == null) {
            return AjaxResult.error("用户账号认证设备");
        }
        return toAjax(gisLayerProductService.updateGisLayerProductSort(products));
    }
}
